package com.example.homework_3;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView activity_text_view;
    Button anonymous_internally_button, activity_button, internally_button, exterior_button, lambda_button, show_system_information, show_progress;

    // 该程序模拟填充长度为100的数组
    private int[] data = new int[100];
    int hasData = 0;
    // 定义进度对话框的标识
    final int PROGRESS_DIALOG = 0x112;
    // 记录进度对话框的完成百分比
    int progressStatus = 0;
    ProgressDialog pd;
    // 定义一个负责更新的进度的Handler
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        anonymous_internally_button = findViewById(R.id.anonymous_internally_button);
        activity_text_view = findViewById(R.id.activity_text_view);
        activity_button = findViewById(R.id.activity_button);
        internally_button = findViewById(R.id.internally_button);
        exterior_button = findViewById(R.id.exterior_button);
        lambda_button = findViewById(R.id.lambda_button);
        show_system_information = findViewById(R.id.show_system_information);
        show_progress = findViewById(R.id.show_progress);

        anonymous_internally_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity_text_view.setText("匿名内部类作为监听器");
            }
        });

        activity_button.setOnClickListener(this);

        internally_button.setOnClickListener(new Internally_Click_Listener());

        exterior_button.setOnClickListener(new Exterior(activity_text_view));

        lambda_button.setOnClickListener((View view) -> {
            activity_text_view.setText("LAMBDA表达式作为监听器");
        });

        show_system_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ConfigurationTest.class);
                startActivity(intent);
            }
        });

        show_progress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(PROGRESS_DIALOG);
            }
        });

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0x123) {
                    pd.setProgress(progressStatus);
                }
            }
        };
    }

    @Override
    public Dialog onCreateDialog(int id, Bundle status) {
        System.out.println("------create------");
        switch (id) {
            case PROGRESS_DIALOG:
                // 创建进度对话框
                pd = new ProgressDialog(this);
                pd.setMax(100);
                // 设置对话框的标题
                pd.setTitle("任务完成百分比");
                // 设置对话框 显示的内容
                pd.setMessage("耗时任务的完成百分比");
                // 设置对话框不能用“取消”按钮关闭
                pd.setCancelable(false);
                // 设置对话框的进度条风格
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                // 设置对话框的进度条是否显示进度
                pd.setIndeterminate(false);
                break;
        }
        return pd;
    }
    @Override
    public void onPrepareDialog(int id, Dialog dialog) {
        System.out.println("------prepare------");
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case PROGRESS_DIALOG:
                // 对话框进度清零
                pd.incrementProgressBy(-pd.getProgress());
                new Thread() {
                    public void run() {
                        while (progressStatus < 100) {
                            // 获取耗时操作的完成百分比
                            progressStatus = doWork();
                            // 发送消息到Handler，请补全代码
                            handler.sendEmptyMessage(0x123);
                        }
                        // 如果任务已经完成
                        if (progressStatus >= 100) {
                            // 关闭对话框
                            pd.dismiss();
                        }
                    }
                }.start();
                break;
        }
    }
    // 模拟一个耗时的操作。
    public int doWork() {
        // 为数组元素赋值
        data[hasData++] = (int) (Math.random() * 100);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hasData;
    }

    public void ClickHere(View source)
    {
        activity_text_view.setText("监听器到标签");
    }

    @Override
    public void onClick(View view) {
        activity_text_view.setText("ACTIVITY作为监听器");
    }

    class Internally_Click_Listener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            activity_text_view.setText("内部类作为监听器");
        }
    }
}